package com.xxbb.springbootapi.service.impl;

import com.xxbb.springbootapi.entity.*;
import com.xxbb.springbootapi.mapper.AccountMapper;
import com.xxbb.springbootapi.service.IAccountService;
import com.xxbb.springbootapi.service.IUserService;
import com.xxbb.springbootapi.util.Pbkdf2Util;
import com.xxbb.springbootapi.util.jwt.JwtUtil;
import com.xxbb.springbootapi.wrapper.AccountQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AccountService extends BaseService<AccountMapper, Account> implements IAccountService {

    @Autowired
    private RoleAuthorityService roleAuthorityService;
    @Autowired
    private AuthorityService authorityService;
    @Autowired
    private UserService userService;

    /**
     * 获取mapper
     *
     * @return
     */
    public AccountMapper getMapper() {
        return super.dao.mapper;
    }

    /**
     * 获取query
     *
     * @return
     */
    public AccountQuery getQuery() {
        return super.dao.mapper.query();
    }

    /**
     * 登录
     *
     * @param account
     * @param password
     * @return
     */
    @Override
    public JsonResult Login(String account, String password) {
        Account obj = getMapper().findOne(getQuery().where.account().eq(account).end());
        if (obj == null) {
            return new JsonResult().Unknown("帐号不存在");
        } else if (!Pbkdf2Util.verify(obj.getPassword(), password)) {
            return new JsonResult().Fail("用户名或密码错误");
        }
        Map map = new HashMap();
        //获取用户权限
        List<RoleAuthority> roleAuthorities = roleAuthorityService.getMapper().listEntity(roleAuthorityService.getQuery().where().roleId().eq(obj.getRoleId()).end());
        List<Integer> ruthorityIdList = roleAuthorities.stream().map(RoleAuthority::getAuthorityId).collect(Collectors.toList());
        List<Authority> authorities = authorityService.getMapper().listEntity(authorityService.getQuery().where().id().in(ruthorityIdList).end());
        List<String> authorityList = authorities.stream().map(Authority::getValue).collect(Collectors.toList());
        //获取用户信息
        User user = userService.getMapper().findById(obj.getUserId());
        //生成token
        Map<String, String> payload = new HashMap<>();
        payload.put("id", obj.getId().toString());//用户id
        payload.put("account", obj.getAccount());//用户账号
        payload.put("authority", authorityList.toString());//用户权限
        payload.put("userId",obj.getUserId().toString());//用户Id
        payload.put("name",user.getName());//用户账号
        String token = JwtUtil.getToken(payload, 60 * 60 * 12);
        return new JsonResultData<>(token).Success();
    }

    @Override
    public boolean add(Account entity) {
        entity.setId(null);//置空用户上传的id
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setIsDeleted(0);
        entity.setPassword(Pbkdf2Util.encryption(entity.getPassword()));//加密密码
        return dao.add(entity) > 0;
    }
}
